//
//  CellOfChat.m
//  气泡聊天Demo
//
//  Created by 米华 on 16/7/11.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "CellOfChat.h"
#import "Masonry.h"
@implementation CellOfChat

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self createSubviews];
        
    }
    return self;
    
}

#pragma mark - 创建子控件
- (void)createSubviews {
    
    self.avatar = [[UIImageView alloc] init];
    
    self.avatar.layer.cornerRadius = 25;
    
    self.avatar.clipsToBounds = YES;
    
    self.avatar.layer.borderWidth = .1;
    
    [self.contentView addSubview:self.avatar];
    
    
    
    self.bubbleView = [[UIImageView alloc] init];
    
    [self.contentView addSubview:self.bubbleView];
    
    
    
    self.contentLabel = [[UILabel alloc] init];
    
    self.contentLabel.numberOfLines = 0;
    
    [self.bubbleView addSubview:self.contentLabel];
    
    
}

#pragma mark - 子控件布局
- (void)layoutSubviews {
    
    [self.avatar mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.trailing.offset(-10);
        
        make.bottom.offset(-10);
        
        make.width.offset(50);
        
        make.height.offset(50);
        
    }];
    
    [self.bubbleView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.avatar.mas_left).offset(5);
        
        make.bottom.offset(-10);
        
        make.width.offset(self.contentLabel.frame.size.width + 20);
        
        make.height.offset(self.contentLabel.frame.size.height + 20);
    }];
//
//    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//       
//        make.leading.offset(10);
//        
//        make.bottom.offset(-10);
//        
//    }];
    
    
}


- (void)ChatCellCommentWithModel:(ModelOfChat *)model {
    
    // 计算文本宽度和高度
    CGRect rec = [model.message boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    
    // 聊天气泡
    UIImage *image = nil;
    
    // 头像
    UIImage *avatar = nil;
    
    image = [UIImage imageNamed:@"bubbleMine"];
    
    avatar = [UIImage imageNamed:@"naruto"];
    
    image = [image stretchableImageWithLeftCapWidth:image.size.width / 2 topCapHeight:image.size.height / 2];
    
    self.bubbleView.image = image;
    
    
    self.avatar.image = avatar;
    
    // 文本内容的frame
    self.contentLabel.frame = CGRectMake(5, 5, rec.size.width, rec.size.height);
    
    self.contentLabel.text = model.message;
    
    
    
}




- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
